<?php 
use solideagle\utilities\SuperEntities;

use solideagle\data_access\Person;
use solideagle\data_access\helpers\DateConverter;

//$ppp = new Person();
$ppp = $this->person; ?>

<script>
	$(function(){
				<?php 
				if($this->state === $this->stateShow){
					?>
					$("#formUser input").attr("disabled", "disabled").css("cursor","default");
					$("#formUser textarea").attr("disabled", "disabled").css("cursor","default");
					$("#formUser button").attr("disabled", "disabled").css("cursor","default");					
                                        loadPicture();
					<?php
				}
				?>
		
				$("#groupbreadcrumbs").load("<?php echo $this->SE_path . '/groups/getgroupbreadcrumbs?groupid=' . $this->group->getId() ?>");
			
				$('#myModal').modal('show');
		
                $(".collapseAdditionalInfo").collapse();

                $( "#txtBirthDate" ).datepicker({ 
                    changeYear: true,
                    changeMonth: true,
                    minDate: new Date(1900, 1 - 1, 1),
                    yearRange: "-100:+0",
                    dateFormat: "dd-mm-yy"
                }).css("cursor","default").attr("readonly","readonly");

                $( "#txtActiveFrom" ).datepicker({ 
                    changeYear: true,
                    changeMonth: true,
                    minDate: new Date(1900, 1 - 1, 1),
                    yearRange: "-100:+0",
                    dateFormat: "dd-mm-yy"
                }).css("cursor","default").attr("readonly","readonly");

                $( "#txtActiveUntill" ).datepicker({ 
                    changeYear: true,
                    changeMonth: true,
                    minDate: new Date(1900, 1 - 1, 1),
                    yearRange: "-100:+0",
                    dateFormat: "dd-mm-yy"
                }).css("cursor","default").attr("readonly","readonly");
                
                $("#txtPictureUrl").focusout(function() {
                    loadPicture();
                });
                
                function loadPicture() {
                    var img = new Image();
                    
                    $(img)
                        .load(function() {
                            $(this).hide();

                            $('#pictureHolder')
                                .empty();
                            $('#pictureHolder')
                                .append(this);

                            $(this).fadeIn();
                    }).attr('src', $("#txtPictureUrl").val())
                    .height('120px').css({"border" : "1px solid #CCCCCC", "border-radius" : "3px 3px 3px 3px"
                    })
                }

		$("#formUser").ajaxForm({
			
			beforeSubmit:  preventDoubleSubmit,
			success: function(responseText, statusText, xhr, $form) { 

				isSubmitting = false;

				if(responseText.length > 0)
				{
					if(responseText.indexOf("GeneratedUsername:") != -1)
					{
						var cutoff = responseText.indexOf(":");
						
						$("#txtUsername").val(responseText.substring(cutoff+1));
						
					}else if(responseText.indexOf("GeneratedPassword:") != -1)
					{
						var cutoff = responseText.indexOf(":");
						$("#txtPassword").val(responseText.substring(cutoff+1));
					}else{
						alert(responseText);
					}
				}else{
					$('#myModal').modal('hide');
					updateUsers();
				}
	    } });

	});

</script>

<form class="form-horizontal" action="<?php echo $this->SE_path . "/users/adduserpost" ?>" method="post" id="formUser">

	<div class="hide SEsupermodal" id="myModal">
		<div>
			<div class="modal-header">
				<a class="close" data-dismiss="modal">×</a>
				<h3 id="modaltitle">

					<?php 
					if($this->state === $this->stateNew)
					{
						echo "Gebruiker maken";
					}else if($this->state === $this->stateEdit){
						echo "Gebruiker bewerken";
					}else if($this->state === $this->stateShow){
						echo "Gebruiker bekijken";
					}
					?>
					<small id="groupbreadcrumbs"></small>
				</h3>
			</div>
			<div class="modal-body">
				<fieldset>

					<div class="row-fluid">

						<div class="span6">

							<div class="control-group">
								<label class="control-label" for="cbActive">Basisrol</label>
								<div class="controls">

									<?php foreach($this->types as $type)
                                        {?>
									<label class="checkbox inline SEinline">
										<input name="ptype[]" value="<?php echo $type->getId(); ?>"
										<?php
										if( //put some checkmarks
												in_array($type,$ppp->getTypes())
												|| ($this->state === $this->stateNew && in_array($type,$this->group->getTypes()))
										)
										{
											echo "checked='checked'";
										}
											
										?>
											type="checkbox">
										<?php echo $type->getTypeName(); ?>
									</label>

									<?php } ?>

								</div>
							</div>


							<div class="control-group">
								<label class="control-label" for="txtFirstName">Voornaam</label>
								<div class="controls">
									<input id="txtFirstName" name="FirstName" class="input-xlarge" type="text"
										value="<?php echo SuperEntities::encode($ppp->getFirstName()) ?>">
								</div>
							</div>

							<div class="control-group">
								<label class="control-label" for="txtName">Naam</label>
								<div class="controls">
									<input id="txtName" name="Name" class="input-xlarge" type="text" value="<?php echo SuperEntities::encode($ppp->getName()) ?>">
								</div>
							</div>

							<div class="control-group">
								<label class="control-label">Geslacht</label>
								<div class="controls">
									<label class="radio inline">
										<input type="radio" <?php if ($ppp->getGender() === "M") echo 'checked="checked"' ?> value="M"
											id="btnGenderMale" name="Gender">
										Man
									</label>
									<label class="radio inline">
										<input type="radio" <?php if ($ppp->getGender() === "V") echo 'checked="checked"' ?> value="V"
											id="btnGenderFemale" name="Gender">
										Vrouw
									</label>
								</div>
							</div>

							<div class="control-group">
								<label class="control-label" for="txtBirthDate">Geboortedatum</label>
								<div class="controls">
									<input id="txtBirthDate" name="BirthDate" class="input-xlarge" type="text"
										value="<?php echo DateConverter::dbDateToDisplayDate($ppp->getBirthDate()) ?>">
								</div>
							</div>

							<div class="control-group">
								<label class="control-label" for="txtEmail">Email adres</label>
								<div class="controls">
									<input id="txtEmail" name="Email" class="input-xlarge" type="text" value="<?php echo SuperEntities::encode($ppp->getEmail()) ?>">
								</div>
							</div>

							<div class="control-group">
								<label class="control-label" for="txtPhone">Telefoonnummer</label>
								<div class="controls">
									<input id="txtPhone" name="Phone" class="input-xlarge" type="text" value="<?php echo SuperEntities::encode($ppp->getPhone()) ?>">
								</div>
							</div>

						</div>

						<div class="span6">

						
						<?php if($this->state === $this->stateEdit)
					{?>
						<div class="control-group">
								<label class="control-label" for="txtUsername">Gebruikersnaam</label>
								<div class="controls">
									<div class="input-append">
										<input id="txtUsername" size="16" name="AccountUsername" readonly="readonly" class="span2" type="text"
											value="<?php echo SuperEntities::encode($ppp->getAccountUsername()) ?>">
									</div>
								</div>
							</div>
					<?php }else{ ?>
					
						<div class="control-group">
								<label class="control-label" for="txtUsername">Gebruikersnaam</label>
								<div class="controls">
									<div class="input-append">
										<input id="txtUsername" size="16" name="AccountUsername" class="span2" type="text"
											value="<?php echo SuperEntities::encode($ppp->getAccountUsername()) ?>"><button type="submit" name="submit" value="generateUsername" id="btnGenerateUsername" class="btn">Genereer!</button>
									</div>
								</div>
							</div>
					
					<?php } ?>
					
						<?php if($this->state === $this->stateEdit)
					{?>
						
							<div class="control-group">
								<label class="control-label" for="txtPassword">Wachtwoord</label>
								<div class="controls">
									<div class="input-append">
										<input id="txtPassword" size="16" <?php /* name="AccountPassword" */ ?> readonly="readonly" class="span2" type="text"
											value="<?php echo SuperEntities::encode($ppp->getAccountPassword()) ?>">
									</div>
								</div>
							</div>
					<?php }else{ ?>
					
						
							<div class="control-group">
								<label class="control-label" for="txtPassword">Wachtwoord</label>
								<div class="controls">
									<div class="input-append">
										<input id="txtPassword" size="16" name="AccountPassword" class="span2" type="text"
											value="<?php echo SuperEntities::encode($ppp->getAccountPassword()) ?>"><button type="submit" name="submit" value="generatePassword" id="btnGeneratePassword" class="btn">Genereer!</button>
									</div>
								</div>
							</div>
					
					<?php } ?>
						
							

						

							<?php /*<div class="control-group">
								<label class="control-label" for="cbActive">Account</label>
								<div class="controls">
									<label class="checkbox">
										<input id="cbActive" <?php if($ppp->getAccountActive()){echo 'checked="checked"';} ?> value="1" name="AccountActive"
											type="checkbox">
										actief
									</label>
								</div>
							</div>*/ ?>

                                                        <div class="control-group">
								<label class="control-label" for="txtPictureUrl">Foto URL</label>
								<div class="controls">
									<input id="txtPictureUrl" name="PictureUrl" class="input-xlarge" type="text"
										value="<?php echo $ppp->getPictureUrl(); ?>">
                                                                        <div id="pictureHolder"></div>
								</div>
							</div>
                                                    
                                                        

							<div class="control-group">
								<label class="control-label" for="txtActiveFrom">Actief vanaf</label>
								<div class="controls">
									<input id="txtActiveFrom" name="AccountActiveFrom" class="input-xlarge" type="text"
										value="<?php echo DateConverter::dbDateToDisplayDate($ppp->getAccountActiveFrom()) ?>">
								</div>
							</div>

							<div class="control-group">
								<label class="control-label" for="txtActiveUntill">Actief tot</label>
								<div class="controls">
									<input id="txtActiveUntill" name="AccountActiveUntill" class="input-xlarge" type="text"
										value="<?php echo DateConverter::dbDateToDisplayDate($ppp->getAccountActiveUntill()) ?>">
								</div>
							</div>
							
							
							<div class="control-group">
								<label class="control-label" for="txtUniqueident">UniqueIdentifier</label>
								<div class="controls">
									<input id="txtUniqueident"
									<?php if($this->state === $this->stateEdit) echo 'readonly="readonly"'; ?>
									 name="uniqueIdentifier" class="input-xlarge" type="text"
										value="<?php echo SuperEntities::encode($ppp->getUniqueIdentifier()) ?>">
								</div>
							</div>
							
							<div class="control-group">
								<label class="control-label" for="txtInformatid">Informat ID</label>
								<div class="controls">
									<input id="txtInformatid" name="informatId" class="input-xlarge" type="text"
										value="<?php echo SuperEntities::encode($ppp->getInformatId()) ?>">
								</div>
							</div>

							<div class="control-group">
								<label class="control-label" for="txtOtherInformation">Extra informatie</label>
								<div class="controls">
									<textarea rows="1" id="txtOtherInformation" name="OtherInformation" class="input-xlarge"><?php echo SuperEntities::encode($ppp->getOtherInformation()) ?></textarea>
								</div>
							</div>

						</div>

					</div>

					<div class="accordion-heading">
						<a href="#collapseOne" data-toggle="collapse" class="accordion-toggle"> Vul verdere gegevens in </a>
					</div>

					<div class="accordion-body collapse" id="collapseOne" style="height: 0px;">

						<div class="accordion-inner">

							<div class="row-fluid">

								<div class="span6">

									<div class="control-group">
										<label class="control-label" for="txtPhone2">Telefoonnummer 2</label>
										<div class="controls">
											<input id="txtPhone2" name="txtPhone2" class="input-xlarge" type="text" value="<?php echo SuperEntities::encode($ppp->getPhone2()) ?>">
										</div>
									</div>

									<div class="control-group">
										<label class="control-label" for="txtMobile">GSM-nummer</label>
										<div class="controls">
											<input id="txtMobile" name="txtMobile" class="input-xlarge" type="text" value="<?php echo SuperEntities::encode($ppp->getMobile()) ?>">
										</div>
									</div>

									<div class="control-group">
										<label class="control-label" for="txtBirthPlace">Geboorteplaats</label>
										<div class="controls">
											<input id="txtBirthPlace" name="txtBirthPlace" class="input-xlarge" type="text" value="<?php echo SuperEntities::encode($ppp->getBirthPlace()) ?>">
										</div>
									</div>

									<div class="control-group">
										<label class="control-label" for="txtNationality">Nationaliteit</label>
										<div class="controls">
											<input id="txtNationality" name="txtNationality" class="input-xlarge" type="text" value="<?php echo SuperEntities::encode($ppp->getNationality()) ?>">
										</div>
									</div>



									<div class="control-group">
										<label class="control-label" for="txtStreet">Straat</label>
										<div class="controls">
											<input id="txtStreet" name="txtStreet" class="input-xlarge" type="text" value="<?php echo SuperEntities::encode($ppp->getStreet()) ?>">
										</div>
									</div>

									<div class="control-group">
										<label class="control-label" for="txtHouseNumber">Huisnummer</label>
										<div class="controls">
											<input id="txtHouseNumber" name="txtHouseNumber" class="input-xlarge" type="text" value="<?php echo SuperEntities::encode($ppp->getHouseNumber()) ?>">
										</div>
									</div>

									<div class="control-group">
										<label class="control-label" for="txtCity">Woonplaats</label>
										<div class="controls">
											<input id="txtCity" name="txtCity" class="input-xlarge" type="text" value="<?php echo SuperEntities::encode($ppp->getCity()) ?>">
										</div>
									</div>
                                                                    
                                                                        <div class="control-group">
										<label class="control-label" for="txtPostCode">Postcode</label>
										<div class="controls">
											<input id="txtCity" name="txtPostCode" class="input-xlarge" type="text" value="<?php echo SuperEntities::encode($ppp->getPostCode()) ?>">
										</div>
									</div>

									<div class="control-group">
										<label class="control-label" for="txtCountry">Land</label>
										<div class="controls">
											<input id="txtCountry" name="txtCountry" class="input-xlarge" type="text" value="<?php echo SuperEntities::encode($ppp->getCountry()) ?>">
										</div>
									</div>

								</div>

								<div class="span6">



									<div class="control-group">
										<label class="control-label" for="txtStamnumber">Stamnummer</label>
										<div class="controls">
											<input id="txtStamnumber" name="txtStamnumber" class="input-xlarge" type="text" value="<?php echo SuperEntities::encode($ppp->getStudentStamNr()) ?>">
										</div>
									</div>

									<div class="control-group">
										<label class="control-label" for="txtPreviousSchool">Vorige school</label>
										<div class="controls">
											<input id="txtPreviousSchool" name="txtPreviousSchool" class="input-xlarge" type="text" value="<?php echo SuperEntities::encode($ppp->getStudentPreviousSchool()) ?>">
										</div>
									</div>

									<div class="control-group">
										<label class="control-label" for="txtParentOccupation">Beroep (ouder)</label>
										<div class="controls">
											<input id="txtParentOccupation" name="txtParentOccupation" class="input-xlarge" type="text" value="<?php echo SuperEntities::encode($ppp->getParentOccupation()) ?>">
										</div>
									</div>



								</div>

							</div>

						</div>
					</div>

					<?php if($this->state === $this->stateNew)
					{?>
						<input type="hidden" name="groupId" value="<?php echo $this->group->getId(); ?>">
					<?php } ?>
					
					<?php if($this->state === $this->stateEdit)
					{?>
						<input type="hidden" name="groupId" value="<?php echo $ppp->getGroupId() ?>">
						<input type="hidden" name="Id" value="<?php echo $ppp->getId(); ?>">
					<?php } ?>
					
				</fieldset>
			</div>
			<div class="modal-footer">

				<?php 
				if($this->state === $this->stateNew)
				{
					?>
				<a href="#" class="btn" data-dismiss="modal">Annuleren</a>
				<button class="btn btn-primary" type="submit">Maken</button>
				<?php
				}else if($this->state === $this->stateEdit){
					?>
				<a href="#" class="btn" data-dismiss="modal">Annuleren</a>
				<button class="btn btn-primary" name="submit" value="edit" type="submit">Bijwerken</button>
				<?php
				}else if($this->state === $this->stateShow){
					?>
				<a href="#" class="btn" data-dismiss="modal">Sluiten</a>
				<?php
				}
				?>


			</div>
		</div>
	</div>

</form>
